package com.online.sessionmanageserver.controller;

import com.alibaba.fastjson.JSON;
import com.online.sessionmanageserver.domain.WeChatUserInfo;
import com.online.sessionmanageserver.domain.WeChatUserInfoSession;
import com.online.sessionmanageserver.service.RedisService;
import com.online.sessionmanageserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: liuqing
 * @Despriction:
 * @Date:Created in 2018/4/24 17:53
 * @Modify By:
 */
@RestController
public class SmsController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private UserService userService;

    final static String ADDRESS = "ADDRESS";

    final static String COMPANY = "-COMPANY";

    final static String TOKEN = "-TOKEN";

    @PostMapping("/getSessionList")
    public Map getSessionList(@RequestBody Map map) {
        System.out.println("...。。。："+map);
        Integer serviceId = (Integer) map.get("service_id");
        Integer eId = (Integer) map.get("e_id");
        Map restMap = new HashMap(16);
        if (StringUtils.isEmpty(serviceId) || StringUtils.isEmpty(eId)) {
            restMap.put("results", 0);
            return restMap;
        }

        Map<Object, Object> agentSessionsMap = redisService.getAgentSessions(serviceId + "");

        //TODO: agentSessionsMap 里面的终端超过两天的话，怎么办？坐席异常掉线后，多久没上来应该把他的聊天会话转移出去？
        if (agentSessionsMap != null && agentSessionsMap.size() != 0) {

            List<WeChatUserInfoSession> wcuisList = redisService.getWeChatUserInfo(agentSessionsMap);
            if (wcuisList == null) {
                restMap.put("results", 0);
            } else {
                restMap.put("session_obj", wcuisList);
                restMap.put("results", wcuisList.size());
            }
        } else {
            restMap.put("results", 0);
        }

        return restMap;
    }

    @PostMapping("/getWeChatUser")
    public Map getWeChatUser(@RequestBody Map map) {
        String openId = (String) map.get("open_id");
        Map<String ,Object> restMap = new HashMap(16);
        if (StringUtils.isEmpty(openId)) {
            restMap.put("result", 1);
            return restMap;
        }
        WeChatUserInfo weChatUserInfo = redisService.getWeChatUserInfo(openId);
        if (weChatUserInfo == null) {
            restMap.put("result", 0);
        } else {
            restMap.put("result", 1);
            restMap.put("open_id",weChatUserInfo.getOpenId());
            restMap.put("name",weChatUserInfo.getName());
            restMap.put("address",weChatUserInfo.getAddress());
            restMap.put("e_id",weChatUserInfo.geteId());
            restMap.put("sex",weChatUserInfo.getSex());
            redisService.expire(openId, 48 * 60L);
        }

        return restMap;
    }

    @PostMapping("/saveWeChatUser")
    public Map saveWeChatUser(@RequestBody Map map) {
        Integer eId = (Integer) map.get("e_id");
        String openId = (String) map.get("open_id");
        Map restMap = new HashMap(16);
        if (StringUtils.isEmpty(eId) || StringUtils.isEmpty(openId)) {
            restMap.put("result", 1);
            return restMap;
        }

        WeChatUserInfo weChatUserInfo = new WeChatUserInfo(openId, (String) map.get("name"), (String) map.get("address"),
                eId, (String) map.get("sex"));
        redisService.add(openId, JSON.toJSONString(weChatUserInfo), 48 * 60L);

        restMap.put("result", 0);
        return restMap;
    }


    @PostMapping("/checkToken")
    public Map checkToken(@RequestBody Map map) {
        Integer service_id = (Integer) map.get("service_id");
        String token = (String) map.get("token");
        Map restMap = new HashMap(16);
        if (!StringUtils.isEmpty(service_id) && !StringUtils.isEmpty(token)) {
            String restToken = redisService.getStr(service_id + TOKEN);
            if (token.equals(restToken)) {
                restMap.put("result", 0);
                return restMap;
            } else {
                restMap.put("result", 1);
                return restMap;
            }
        }
        restMap.put("result", 1);
        return restMap;
    }

    @PostMapping("/saveToken")
    public Map saveToken(@RequestBody Map map) {
        Integer serviceId = (Integer) map.get("service_id");
        String token = (String) map.get("token");
        Map restMap = new HashMap(16);
        if (!StringUtils.isEmpty(serviceId) && !StringUtils.isEmpty(token)) {
            redisService.add(serviceId + TOKEN, token, 24 * 60L);


            restMap.put("result", 0);
            return restMap;
        }
        restMap.put("result", 1);
        return restMap;
    }

    @PostMapping("/saveWsAddress")
    public Map saveWsAddress(@RequestBody Map map) {
        Integer serviceId = (Integer) map.get("service_id");
        String address = (String) map.get("address");
        Map restMap = new HashMap(16);
        if (!StringUtils.isEmpty(serviceId) && !StringUtils.isEmpty(address)) {
            redisService.add(serviceId + ADDRESS, address);

            List<Integer> cIds = userService.getCId(serviceId);
            if (!StringUtils.isEmpty(cIds)) {
                for (int i = 0; i < cIds.size(); i++) {
                    redisService.addSet(cIds.get(i) + COMPANY, serviceId + "");
                }
            }

            restMap.put("result", 0);
            return restMap;
        }
        restMap.put("result", 1);
        return restMap;
    }

    @PostMapping("/getMessages")
    public Map getMessages(@RequestBody Map map) {
        Integer serviceId = (Integer) map.get("service_id");
        Integer sessionId = (Integer) map.get("session_id");
        Map restMap = new HashMap(16);
        if (!StringUtils.isEmpty(serviceId) && !StringUtils.isEmpty(sessionId)) {
            List<String> restStr = redisService.getMessageList(sessionId + "");
            if (restStr != null && restStr.size() != 0) {
                restMap.put("messages", restStr);
            } else {
                restMap.put("messages", new ArrayList<String>());
            }

        }
        return restMap;
    }

    @PostMapping("/getWsAddress")
    public Map getWsAddress(@RequestBody Map map) {
        Integer serviceId = (Integer) map.get("service_id");
        Map restMap = new HashMap(16);
        if (!StringUtils.isEmpty(serviceId)) {
            String rest = redisService.getStr(serviceId + ADDRESS);
            if (!StringUtils.isEmpty(rest)) {
                restMap.put("result", 0);
                restMap.put("address", rest);
            } else {
                restMap.put("result", 1);
            }
        } else {
            restMap.put("result", 1);
        }
        return restMap;
    }

    @PostMapping("/delWsAddress")
    public Map delWsAddress(@RequestBody Map map) {
        Integer serviceId = (Integer) map.get("service_id");
        Map restMap = new HashMap(16);
        if (StringUtils.isEmpty(serviceId)) {
            redisService.delete(serviceId + ADDRESS);
            List<Integer> cIds = userService.getCId(serviceId);
            if (!StringUtils.isEmpty(cIds)) {
                for (int i = 0; i < cIds.size(); i++) {
                    redisService.delSet(cIds.get(i) + COMPANY, serviceId + "");
                }
            }
        }
        restMap.put("result", 0);
        return restMap;
    }
}
